using System;
using System.ComponentModel;
using System.IO;
using RollManager.Library.Utils;

namespace RollManager.Library.ActionSteps
{
    [Serializable]
    public class ListDirectoryStep : ActionStep
    {
        private string m_srcDir = StepConstants.PATH;
        private string m_filter = StepConstants.FILTER;
        private bool m_recursive = false;

        [DisplayName( "File Filter" )]
        [Description( "The file filter to use when listing files in the source directory." )]
        public string Filter
        {
            get
            {
                return m_filter;
            }
            set
            {
                if( string.IsNullOrEmpty( value ) )
                {
                    throw new ArgumentException( "Filter cannot be empty.  Use *.* for a global wildcard." );
                }
                else
                {
                    m_filter = value.Trim();
                }
            }
        }

        [Description( "Whether the listing should only apply to the top level source directory or to all children directories as well." )]
        public bool Recursive
        {
            get
            {
                return m_recursive;
            }
            set
            {
                m_recursive = value;
            }
        }

        [DisplayName( "Source Dir" )]
        [Description( "The source directory path from where files are listed." )]
        public string SrcDir
        {
            get
            {
                return m_srcDir;
            }
            set
            {
                if( string.IsNullOrEmpty( value ) )
                {
                    throw new ArgumentException( "Source Dir cannot be empty." );
                }
                else
                {
                    m_srcDir = value.Trim();
                }
            }
        }

        public override string Name
        {
            get { return "List Directory"; }
        }

        private SearchOption RecursionOption
        {
            get
            {
                return Recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
            }
        }

        protected override void Run()
        {
            // Log each directory found in creation date order
            foreach( DirectoryInfo subDir in Common.ListDirectories( m_srcDir, m_filter, RecursionOption ) )
            {
                Progress.Add( subDir.Name );
            }
        }

        protected override void Simulate()
        {
            // Check for source dir
            DirectoryInfo dir = new DirectoryInfo( m_srcDir );
            if( !dir.Exists )
            {
                throw new Exception( string.Format( "Source dir {0} does not exist.", m_srcDir ) );
            }

            if( string.IsNullOrEmpty( m_filter ) )
            {
                throw new Exception( "Filter was empty." );
            }

            Progress.AddFormat( "Simulated listing of directory {0} with filter {1}", m_srcDir, m_filter );
        }
    }
}
